/*******************************************************************************
 * Copyright 2010 DemandSpot
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.demandspot.utils;

import java.lang.management.ManagementFactory;

import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;

import org.apache.log4j.Logger;

public class JmxUtils {

	private final static Logger logger = Logger.getLogger(JmxUtils.class);
	
	private static final String SERVER_DELEGATE = "JMImplementation:type=MBeanServerDelegate";
	private static final String MBEAN_SERVER_ID_KEY = "MBeanServerId";

	
	public static String getPlatformMBeanServerId() {
		MBeanServer server = ManagementFactory.getPlatformMBeanServer();
		String serverId = getMBeanServerId(server);
		return serverId;
	}
	
	/**
	 * Get the MBeanServerId of Agent ID for the provided MBeanServer.
	 * 
	 * @param mbeanServer MBeanServer whose Server ID/Agent ID is desired.
	 * @return MBeanServerId/Agent ID of provided MBeanServer.
	 */
	public static String getMBeanServerId(final MBeanServer mbeanServer) {
		
		String serverId = null;
		try {
			ObjectName delegateObjName = new ObjectName(SERVER_DELEGATE);
			serverId = (String) mbeanServer.getAttribute(delegateObjName, MBEAN_SERVER_ID_KEY);
		} catch (MalformedObjectNameException malformedObjectNameException) {
			System.err.println("Problems constructing MBean ObjectName: "+ malformedObjectNameException.getMessage());
		} catch (AttributeNotFoundException noMatchingAttrException) {
			logger.error("Unable to find attribute " + MBEAN_SERVER_ID_KEY + " in MBean " + SERVER_DELEGATE	+ ": " + noMatchingAttrException);
		} catch (MBeanException mBeanException) {
			logger.error("Exception thrown by MBean's (" + SERVER_DELEGATE + "'s " + MBEAN_SERVER_ID_KEY + ") getter: " + mBeanException.getMessage());
		} catch (ReflectionException reflectionException) {
			logger.error("Exception thrown by MBean's (" + SERVER_DELEGATE + "'s " + MBEAN_SERVER_ID_KEY + ") setter: " + reflectionException.getMessage());
		} catch (InstanceNotFoundException noMBeanInstance) {
			logger.error("No instance of MBean " + SERVER_DELEGATE + " found in MBeanServer: " + noMBeanInstance.getMessage());
		}
		return serverId;
	}

}
